<?php
include './php-ofc-library/open-flash-chart.php';

$link=mysql_connect ("localhost","sun","") or die ("Can not connect to db!".mysql_error());
mysql_select_db("jcat",$link) or die ("Can not select db!".mysql_error());


$Params = $_GET['Params'];
list($chart, $trader, $traderNo, $specialist, $level1, $level2, $game, $items, $start, $status) = split('[,]', $Params);

$traderRole = substr($trader, 0,3);

$queryTrader = " AND traderId LIKE '".$trader."%'";

/*
if($traderRole=='buy'){
	$queryTrader = " AND traderType LIKE 'buyer%' ";
	$trader = 'Buyer';
}else if($traderRole=='sel'){
	$queryTrader = " AND traderType LIKE 'seller%' ";
	$trader = 'Seller';
}
*/

$Experiment = $level1.'_'.$level2.'_'.$game;

$items =(int)$items;
$step = 1;
if($items>20) $step = floor($items/20);

$accumulating = (int)$start+1;
$final = (int)$start + (int)$items -1;
$xLables = array();
while ( $accumulating <= ($items+(int)$start+1) )
{
	$xLables[] = $accumulating;
	$accumulating=$accumulating+$step;
}

if($specialist!=''){
	$querySpecialist = " AND specialist= '".$specialist."' ";
}else{
	$querySpecialist = "";
}

	$sql="SELECT `day`, `traderType`,count(`traderId`) as numTrader FROM x_traderLog WHERE ExperimentId = '".$Experiment."' " . $querySpecialist . $queryTrader. "  GROUP BY day, traderType ORDER BY day, traderType ASC ;";

	$result = mysql_query($sql,$link) or die ("Can not read db!".mysql_error());

	$trderGD = array();
	$traderRE = array();
	$traderZIC = array();
	$traderZIP = array();
	
	for($i=0;$i<=$items;$i++){
		$traderGD[$i] = 0;
		$traderRE[$i] = 0;
		$traderZIP[$i] = 0;
		$traderZIC[$i] = 0;
	}


	while($row = mysql_fetch_array($result, MYSQL_ASSOC))
	{	
		$d= $row['day'];
		if($traderRole=='buy'){
		  switch ($row['traderType']) {
		    case 'buyer_gd':
			$traderGD[$d] = round($row['numTrader']);	
			break;
		    case 'buyer_re':
			$traderRE[$d] = round($row['numTrader']);	
			break;
		    case 'buyer_zip':
			$traderZIP[$d] = round($row['numTrader']);
			break;
		    case 'buyer_zic':
			$traderZIC[$d] = round($row['numTrader']);	
			break;	
		  }	
		}else{
		  switch ($row['traderType']) {
			case 'seller_gd':
			$traderGD[$d] = round($row['numTrader']);	
			break;
		    case 'seller_re':
			$traderRE[$d] = round($row['numTrader']);	
			break;
		    case 'seller_zip':
			$traderZIP[$d] = round($row['numTrader']);	
			break;
		    case 'seller_zic':
			$traderZIC[$d] = round($row['numTrader']);	
			break;
		  }
		}
	} 
/*
$star = new star();
$star->size(6)->halo_size(2)->colour('#f00000')->rotation(180)->hollow(false);

$bow = new bow();
$bow->size(6)->halo_size(0)->colour('#3D5C56')->rotation(90);
*/

$line_trader_gd = new line_dot();
$line_trader_gd->set_width( 2 );
$line_trader_gd->set_colour( '#0099cc' );
$line_trader_gd->set_dot_size( 3 );
$line_trader_gd->set_values( $traderGD );
$line_trader_gd->set_tooltip( 'GD: #val#' );
$line_trader_gd->set_key( "GD", 10 );

$line_trader_re = new line_dot();
$line_trader_re->set_width( 2 );
$line_trader_re->set_colour( '#ff6666' );
$line_trader_re->set_dot_size( 3 );
$line_trader_re->set_values( $traderRE );
$line_trader_re->set_tooltip( 'RE: #val#' );
$line_trader_re->set_key( "RE", 10 );

$line_trader_zip = new line_hollow();
$line_trader_zip->set_width( 2 );
$line_trader_zip->set_colour( '#666666' );
$line_trader_zip->set_dot_size( 3 );
$line_trader_zip->set_values( $traderZIP );
$line_trader_zip->set_tooltip( 'ZIP: #val#' );
$line_trader_zip->set_key( "ZIP", 10 );

$line_trader_zic = new line_hollow();
$line_trader_zic->set_width( 2 );
$line_trader_zic->set_colour( '#cc9999' );
$line_trader_zic->set_dot_size( 3 );
$line_trader_zic->set_values( $traderZIC );
$line_trader_zic->set_tooltip( 'ZIC: #val#' );
$line_trader_zic->set_key( "ZIC", 10 );


$chart = new open_flash_chart();

$chart->set_title( new title( $Experiment.' - '.$trader.' in '.$specialist.' ('.$start.' - '.$final.')' ) );

$x_legend = new x_legend( 'Day' );
$x_legend->set_style( '{font-size: 12px; color: #333333}' );
$chart->set_x_legend( $x_legend );

$y_legend = new y_legend( 'Number of Trader' );
$y_legend->set_style( '{font-size: 12px; color: #333333}' );
$chart->set_y_legend( $y_legend );

$x_labels = new x_axis_labels();
$x_labels->set_steps( $step );
//$x_labels->set_vertical();
$x_labels->set_colour( '#000000' );
//$x_labels->set_labels( $items );

$x = new x_axis();
$x->set_colour( '#333333' );
//$x->set_grid_colour( '#D7E4A3' );
$x->set_offset( false );
$x->set_steps( $step );
$x->set_range( $start, $final+1 );

$chart->set_x_axis( $x );

$maxtraderGD = max($traderGD);
$maxtraderRE = max($traderRE);
$maxtraderZIC = max($traderZIC);
$maxtraderZIP = max($traderZIP);

$maxY = max($maxtraderGD, $maxtraderRE, $maxtraderZIC, $maxtraderZIP);
$maxY = ceil($maxY * 10)/10;

//$divisorY = $maxY / 20 ;
$stepY = ceil($maxY / 10);

$y = new y_axis();
$chart->set_y_axis( $y );
$y->set_range( 0, $maxY, $stepY ); 

//
// here we add our data sets to the chart:
//
$chart->add_element( $line_trader_gd );
$chart->add_element( $line_trader_re );
$chart->add_element( $line_trader_zip );
$chart->add_element( $line_trader_zic );


echo $chart->toPrettyString();

?>
